package com.hq.lib_comview.common.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.hq.lib_comview.view.glide.GlideCenterRoundImage;
import com.hq.lib_comview.view.glide.GlideCircleImage;
import com.hq.lib_comview.view.glide.GlideRoundImage;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 描述 ：图片操作工具
 * 作者 ：hq
 * 时间 ：2017/8/5 14:39
 */
public class ImageUtils {
    /**
     * 获取图片后缀名称
     *
     * @param picturePath
     * @return
     */
    public static String getPicNameFromPath(String picturePath) {
        String temp[] = picturePath.replaceAll("\\\\", "/").split("/");
        String fileName = "";
        if (temp.length > 1) {
            fileName = temp[temp.length - 1];
        }
        return fileName;
    }

    /**
     * 显示本地图片
     *
     * @param url
     * @return
     */
    public static Bitmap getLoacalBitmap(String url) {
        if (StringUtils.isEmpty(url)) {
            return null;
        }
        try {
            FileInputStream fis = new FileInputStream(url);
            return BitmapFactory.decodeStream(fis);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获得实际的高度
     *
     * @param view
     * @return
     */
    public static int getMeasureHeight(View view) {
        int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
        view.measure(w, h);
        int height = view.getMeasuredHeight();
        return height;
    }

    /**
     * 获得实际的宽度
     *
     * @param view
     * @return
     */
    public static int getMeasureWidth(View view) {
        int w = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);
        view.measure(w, h);
        int width = view.getMeasuredWidth();
        return width;
    }

    /**
     * 根据时间来命名图片名字 如：15028998329.jpg
     *
     * @return
     */
    public static String getPhotoFileName() {
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "'IMG'_yyyyMMdd_HHmmss");
        return dateFormat.format(date) + ".jpg";
    }

    /**
     * 显示网络图片
     *
     * @param mContext
     * @param picUrl
     * @param loadingImg
     * @param faildImg
     * @param mShowImg
     */
    public static void showImg(Context mContext, String picUrl, int loadingImg, int faildImg, ImageView mShowImg) {
        if (mContext instanceof Activity) {
            Activity act = (Activity) mContext;
            if (act == null || act.isFinishing()) {
                return;
            }
        }
        if (Protect.checkLoadImageStatus(mContext)) {
            Glide.with(mContext).load(picUrl).placeholder(loadingImg).error(faildImg).into(mShowImg);
        }
    }

    /**
     * 显示圆形图片
     *
     * @param mContext
     * @param picUrl
     * @param loadingImg
     * @param faildImg
     * @param mShowImg
     */
    public static void showCircleImg(Context mContext, String picUrl, int loadingImg, int faildImg, ImageView mShowImg) {
        if (mContext instanceof Activity) {
            Activity act = (Activity) mContext;
            if (act == null || act.isFinishing()) {
                return;
            }
        }
        if (Protect.checkLoadImageStatus(mContext)) {
            Glide.with(mContext).load(picUrl).transform(new GlideCircleImage(mContext)).
                    placeholder(loadingImg).error(faildImg).crossFade().into(mShowImg);
        }
    }

    /**
     * 显示正方形四个角圆角图片
     *
     * @param mContext
     * @param picUrl
     * @param loadingImg
     * @param faildImg
     * @param mShowImg
     */
    public static void showRoundImg(Context mContext, String picUrl, int loadingImg, int faildImg, ImageView mShowImg) {
        if (mContext instanceof Activity) {
            Activity act = (Activity) mContext;
            if (act == null || act.isFinishing()) {
                return;
            }
        }
        if (Protect.checkLoadImageStatus(mContext)) {
            Glide.with(mContext).load(picUrl).transform(new GlideRoundImage(mContext)).
                    placeholder(loadingImg).error(faildImg).crossFade().into(mShowImg);
        }
    }

    /**
     * 显示
     *
     * @param mContext
     * @param picUrl
     * @param loadingImg
     * @param faildImg
     * @param mShowImg
     */
    public static void showCenterRoundImg(Context mContext, String picUrl, int loadingImg, int faildImg, ImageView mShowImg) {
        if (mContext instanceof Activity) {
            Activity act = (Activity) mContext;
            if (act == null || act.isFinishing()) {
                return;
            }
        }
        if (Protect.checkLoadImageStatus(mContext)) {
            Glide.with(mContext).load(picUrl).transform(new GlideCenterRoundImage(mContext)).
                    placeholder(loadingImg).error(faildImg).crossFade().into(mShowImg);
        }
    }

    /**
     * 显示gif
     *
     * @param mContext
     * @param loadingImg
     * @param mShowImg
     */
    public static void showGifImg(Context mContext, int drawable, int loadingImg, ImageView mShowImg) {
        if (mContext instanceof Activity) {
            Activity act = (Activity) mContext;
            if (act == null || act.isFinishing()) {
                return;
            }
        }
        if (Protect.checkLoadImageStatus(mContext)) {
            Glide.with(mContext).load(drawable).asGif().diskCacheStrategy(DiskCacheStrategy.SOURCE).placeholder(loadingImg).error(loadingImg).into(mShowImg);
        }
    }

}
